package com.bop.dehong.login;

import java.util.List;


import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;

import com.base.dehong.http.RequestCallBack;
import com.base.dehong.util.ToastUtils;
import com.bop.dehong.R;
import com.bop.dehong.base.BaseActivity;
import com.bop.dehong.base.TopManager;
import com.bop.dehong.config.Config;
import com.bop.dehong.http.model.MyIndentityModel;
import com.bop.dehong.http.model.SaveMyIndentityModel;
import com.bop.dehong.info.EmptyInfo;
import com.bop.dehong.info.entity.IdentiyEntity;
import com.bop.dehong.info.entity.IdentiyListEntity;

/**
 * 身份选择
* @author TangWenhan/YeFengyun
*/
public class MyIdentityActitvity extends BaseActivity {
	
	private TextView myidentity_iv1, myidentity_iv2, myidentity_iv3, myidentity_iv4, myidentity_iv5;
	private MyIndentityModel myIndentityModel;
	private SaveMyIndentityModel saveMyIndentityModel;
	private List<IdentiyEntity> identity;
	private String uid, identityid;
	private String mUserNameStr;
	private String mPasswordStr;
	private Intent intent;
	
	@Override
	protected void initTop() {
		View mView = getView(R.id.myidentity_top);
		TopManager manager = new TopManager(mView);
		manager.setTitle("我的身份是...");
		manager.setLeftImageVisibility(View.VISIBLE);
	}

	@Override
	protected void initUI() {
		myidentity_iv1 = getView(R.id.myidentity_iv1);
		myidentity_iv2 = getView(R.id.myidentity_iv2);
		myidentity_iv3 = getView(R.id.myidentity_iv3);
		myidentity_iv4 = getView(R.id.myidentity_iv4);
		myidentity_iv5 = getView(R.id.myidentity_iv5);
		registerOnClickListener(myidentity_iv1, myidentity_iv2,
				myidentity_iv3, myidentity_iv4, myidentity_iv5);
		intent = getIntent();
		Bundle bundle = intent.getExtras();
		uid = bundle.getString(Config.UID);
		mUserNameStr = bundle.getString("username");
		mPasswordStr = bundle.getString("password");
		
	}

	@Override
	protected void initData() {
		showProgressDialog();
		if (myIndentityModel == null) {
			myIndentityModel = new MyIndentityModel(this, callback);
		}
		myIndentityModel.doNet();
	}

	RequestCallBack<IdentiyListEntity> callback = new RequestCallBack<IdentiyListEntity>() {

		@Override
		public void end() {
			disappearProgressDialog();
		}
		
		public void onSuccess(IdentiyListEntity info) {
			identity = info.identity;
			myidentity_iv1.setText(identity.get(0).getName());
			myidentity_iv2.setText(identity.get(1).getName());
			myidentity_iv3.setText(identity.get(2).getName());
			myidentity_iv4.setText(identity.get(3).getName());
			myidentity_iv5.setText(identity.get(4).getName());
			super.onSuccess(info);
		};
		
	};
	
	private void save(String identityid) {
		if (saveMyIndentityModel == null) {
			saveMyIndentityModel = new SaveMyIndentityModel(this, callback1);
		}
		saveMyIndentityModel.doNet(uid, identityid);
	}

	RequestCallBack<EmptyInfo> callback1 = new RequestCallBack<EmptyInfo>() {

		@Override
		public void end() {
			disappearProgressDialog();
		}
		
		public void onSuccess(EmptyInfo info) {
			super.onSuccess(info);
			ToastUtils.show(MyIdentityActitvity.this, "身份保存成功！");
			Intent intent = new Intent();
			Bundle bundle = new Bundle();
			bundle.putString("username", mUserNameStr);
			bundle.putString("password", mPasswordStr);
			intent.putExtras(bundle);
			setResult(RESULT_OK, intent);
			finishCurrentActivity();
		};
		
	};
	
	@Override
	protected int getContentViewId() {
 		TAG = getClass().getName();
		return R.layout.act_myidentity;
	}

	@Override
	public void onClick(View v)
	{
		switch (v.getId()) {
		case R.id.myidentity_iv1:
			identityid = identity.get(0).getId();
			save(identityid);
			break;
		case R.id.myidentity_iv2:
			identityid = identity.get(1).getId();
			save(identityid);
			break;
		case R.id.myidentity_iv3:
			identityid = identity.get(2).getId();
			save(identityid);
			break;
		case R.id.myidentity_iv4:
			identityid = identity.get(3).getId();
			save(identityid);
			break;
		case R.id.myidentity_iv5:
			identityid = identity.get(4).getId();
			save(identityid);
			break;
		default:
			break;
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id)
	{
		// TODO Auto-generated method stub
		
	}

}
